package com.metagurukul.metaspace.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.models.beans.UserPersonalDetail;
import com.metagurukul.metaspace.util.Constants;

/**
 * @author ritesh
 *
 */
public class UserPersonalDetailDao extends DatabaseDAO {
	/**
	 * @return
	 * @throws SQLException
	 */
	public List<UserPersonalDetail> getRecordList ()
	throws SQLException {
		ArrayList<UserPersonalDetail> userPersonalDetailList = null;
		String query = "Select * from user_personal_detail";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			
			rs = ps.executeQuery();
			userPersonalDetailList = new ArrayList<UserPersonalDetail>();
			while (rs.next()) {
				UserPersonalDetail userPersonalDetail = fillBean();
				userPersonalDetailList.add(userPersonalDetail);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return userPersonalDetailList;
		
	}
	
	/**
	 * @param userSearchTxt
	 * @return
	 * @throws SQLException
	 */
	public List<UserPersonalDetail> getPersonalRecordList (String userSearchTxt)
	throws SQLException {
		
		ArrayList<UserPersonalDetail> userPersonalDetailList = null;
		if(userSearchTxt.equals("")) {
			return null;
		}
		String query = "Select * from user_personal_detail where name like ?";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setString(1,"%"+userSearchTxt+"%");
			rs = ps.executeQuery();
			userPersonalDetailList = new ArrayList<UserPersonalDetail>();
			while (rs.next()) {
				UserPersonalDetail userPersonalDetail = fillBean();
				userPersonalDetailList.add(userPersonalDetail);
			}
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		return userPersonalDetailList;
		
	}
	
	/**
	 * @return
	 * @throws SQLException
	 */
	private UserPersonalDetail fillBean() throws SQLException{
		UserPersonalDetail userPersonalDetail = new UserPersonalDetail();
		userPersonalDetail.setUserId(rs.getInt("user_id"));
		userPersonalDetail.setContact(rs.getString("contact"));
		userPersonalDetail.setDate(rs.getDate("dob"));
		userPersonalDetail.setGender(rs.getString("gender").charAt(0));
		userPersonalDetail.setName(rs.getString("name"));
		userPersonalDetail.setProfilePicPath(rs.getString("profile_pic_path"));
		userPersonalDetail.setUserName(rs.getString("username"));
		return userPersonalDetail;
	}
	
	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public UserPersonalDetail getRecord(int userId)
	throws SQLException {
		String query = "Select * from user_personal_detail where user_id = ? ";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			/**
			 * set variables in prepared statement
			 * 
			 */
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			if (rs.next()) {
				return fillBean();
			}
			return null;
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
		
	}
	
	/**
	 * @param userPersonalDetail
	 * @return
	 * @throws SQLException
	 */
	public int addRecord(UserPersonalDetail userPersonalDetail) throws SQLException {
		if(!checkIsPresent(userPersonalDetail.getUserId())){
			return insertRecord(userPersonalDetail);
		}
		else{
			return updateRecord(userPersonalDetail);
		}
	}
	
	/**
	 * @param path
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public int updateProfilePic(String path, int userId) throws SQLException{
		String query = "update user_personal_detail set profile_pic_path=? where user_id =?";
		try {
			getConnection();
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, path);
			ps.setInt(2, userId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param userPersonalDetail
	 * @return
	 * @throws SQLException
	 */
	private int updateRecord(UserPersonalDetail userPersonalDetail) throws SQLException {
		getConnection();
		String query = "update user_personal_detail set contact=?, gender=? , dob=? , name =? , username=? where user_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userPersonalDetail.getContact());
			ps.setString(2, userPersonalDetail.getGender()+"");
			ps.setDate(3, userPersonalDetail.getDate());
			ps.setString(4, userPersonalDetail.getName());
			ps.setString(5, userPersonalDetail.getUserName());
			ps.setInt(6, userPersonalDetail.getUserId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}

	/**
	 * @param userPersonalDetail
	 * @return
	 * @throws SQLException
	 */
	private int insertRecord(UserPersonalDetail userPersonalDetail) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "insert into user_personal_detail (contact, gender, dob, name, profile_pic_path, username , user_id) values(?, ?, ?, ?, ?, ? , ?)";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setString(1, userPersonalDetail.getContact());
			ps.setString(2, userPersonalDetail.getGender()+"");
			ps.setDate(3, userPersonalDetail.getDate());
			ps.setString(4, userPersonalDetail.getName());
			ps.setString(5, userPersonalDetail.getProfilePicPath());
			ps.setString(6, userPersonalDetail.getUserName());
			ps.setInt(7, userPersonalDetail.getUserId());
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}

	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	private boolean checkIsPresent(int userId) throws SQLException {
		getConnection();
		String query = "select 1 from user_personal_detail where user_id=?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			rs = ps.executeQuery();
			/**
			 * if there is record in resultset then return true else return
			 * false
			 */
			return rs.next();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
	}
	
	/**
	 * @param userId
	 * @return
	 * @throws SQLException
	 */
	public int deleteRecord(int userId) throws SQLException {
		/* get the connection */
		getConnection();
		String query = "delete from user_personal_detail where user_id =?";
		try {
			ps = con.prepareStatement(query);
			// set variables in prepared statement
			ps.setInt(1, userId);
			return ps.executeUpdate();
			
		} catch (SQLException e) {
			throw new SQLException(Constants.SQL_QUERY_ERROR + e);
		} finally {
			/* close connection */
			closeConnection();
		}
		
	}
}
